$(function () {

    var insurance_datagrid = $("#insurance_datagrid");
    var insurance_dialog = $("#insurance_dialog");
    var insrance_form = $("#insrance_form");

    var methodObj = {
        //取消按钮
        cancel: function () {
            insurance_dialog.dialog("close");
        }
    }

    //事件统一绑定
    $("[data-cmd]").click(function () {

        var cmd = $(this).data("cmd");
        methodObj[cmd]();

    });

    insurance_datagrid.datagrid({
        fit: true,
        rownumbers: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#insurance_toolbar',
        url: '/insuranceTS/queryHistory.do',
        onDblClickRow: function (index, row) {
            //投保人数据回显
            row["client.id"] = row.client.id
            row["client.name"] = row.client.name;
            row["client.sex"] = row.client.sex;
            row["client.tel"] = row.client.tel;
            row["client.id_card"] = row.client.id_card;
            row["client.address"] = row.client.address;

            //车辆信息回显
            row["carInformation.id"] = row.carInformation.id;
            row["carInformation.name"] = row.carInformation.name;
            row["carInformation.brand"] = row.carInformation.brand;
            row["carInformation.type"] = row.carInformation.type;
            row["carInformation.carNumber"] = row.carInformation.carNumber;
            row["carInformation.airVolume"] = row.carInformation.airVolume;
            row["carInformation.state"] = row.carInformation.state;
            row["carInformation.inputTime"] = row.carInformation.inputTime;
            row["carInformation.evaluation"] = row.carInformation.evaluation;
            row["carInformation.carType"] = row.carInformation.carType;
            row["row.productItem.id"] = row.productItem.id

            $("#mech_combo").combobox("setValue", row.productItem.id);

            $.get("/insuranceTS/getCarproductByInsuId.do", {InsuId: row.id}, function (data) {
                $("#product_table").datagrid({
                    data: data,
                    fit: true,
                    fitColumns: true,
                    striped: true,
                    singleSelect: true,
                    columns: [[
                        {field: 'id', title: 'id', width: 100, hidden: true},
                        {field: 'sn', title: '保险编号', width: 100},
                        {field: 'name', title: '保险名称', width: 100},
                        {
                            field: 'mech', title: '保险机构', width: 100, formatter: function (value, row, index) {

                                return row.mech != null ? row.mech.name : ""
                            }
                        },
                        {field: 'insured', title: '保额', width: 100},
                        {field: 'moneyYear', title: '基本年费', width: 100},
                        {
                            field: 'state', title: '销售状态', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>在售</font>" : "<font color='red'>停售</font>"
                            }
                        },
                        {
                            field: 'deductState', title: '不计免赔', width: 100, formatter: function (value, row, index) {
                                return value ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                            }
                        },
                    ]],

                })
                count();
            })
            insrance_form.form('load', row);
            insurance_dialog.dialog("setTitle", "编辑保单");
            insurance_dialog.dialog("open");
        },
        columns: [[
            {field: 'sn', title: '保单编号', width: 100},
            {
                field: 'client', title: '投保人', width: 100,
                formatter: function (value, row, index) {
                    return value.name ? value.name : "";
                }
            },
            {
                field: 'carInformation', title: '车牌号', width: 100,
                formatter: function (value, row, index) {
                    //console.log(row)
                    return value.carNumber ? value.carNumber : "无";
                }
            },
            {field: 'inputtime', title: '开始日期', width: 100},
            {field: 'verifytime', title: '核保日期', width: 100},
            {field: 'verifytime', title: '保险截止日', width: 100},
            {field: 'term', title: '保险时长(/年)', width: 100},
            {
                field: 'saleMan', title: '业务员', width: 100,
                formatter: function (value, row, index) {
                    return row.client.takeUser != null ? row.client.takeUser.realname : "没有人"
                }
            },
            {
                field: 'verifyUser', title: '审核人', width: 100,
                formatter: function (value, row, index) {
                    console.log(row);
                    console.log("----")
                    console.log(value);
                    return value.realname ? value.realname : "没有人"
                }
            },
            {field: 'amount', title: '投保总金额', width: 100},
            {
                field: 'state', title: '状态', width: 100,
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "<font style='color: green'>暂存</font>";
                    } else if (value == 1) {
                        return "<font style='color: red'>待审核</font>";
                    } else if (value == -1) {
                        return "<font style='color: red'>拒保</font>";
                    } else if (value == -2) {
                        return "<font style='color: red'>批改</font>";
                    } else if (value == 3) {
                        return "<font style='color: green'>已缴费</font>";
                    }
                }
            },
        ]]

    });

    //初始化弹出框
    $("#insurance_dialog").dialog({
        width: 800,
        height: 600,
        buttons: '#buttons',
        constrain: true,
        closed: true
    });
})

function count() {

    var value = $("#term").textbox("getValue");
    var totalMoney = 0;
    var rows = $("#product_table").datagrid('getRows');

    for (var i = 0; i <= rows.length - 1; i++) {
        var moneyYear = rows[i].moneyYear;
        totalMoney = totalMoney + value * moneyYear;
    }
    $("#totalMoney").text(totalMoney);
}